package com.atguigu.callable;


import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;


class MyThread implements Callable<Integer>
{
    @Override
    public Integer call() throws Exception
    {
        System.out.println("----come in call method");
        //暂停几秒钟线程
        try { TimeUnit.SECONDS.sleep(3); } catch (InterruptedException e) { e.printStackTrace(); }
        return 1229;
    }
}
public class CallableDemo
{
    public static void main(String[] args) throws ExecutionException, InterruptedException
    {

        /*FutureTask futureTask = new FutureTask(new MyThread());
        Thread t = new Thread(futureTask,"t");
        t.start();*/
        FutureTask futureTask1 = new FutureTask(() -> {

            System.out.println("费时任务");
            try {
                TimeUnit.SECONDS.sleep(3);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }, 6);
        new Thread(futureTask1,"t1").start();


        /*FutureTask futureTask2 = new FutureTask(new MyThread());
        Thread t2 = new Thread(futureTask2,"t2");
        t2.start();*/

        System.out.println(Thread.currentThread().getName()+"主线任务");
        System.out.println(futureTask1.get());

    }
}
